package com.wholesmart.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wholesmart.common.bean.Page;
import com.wholesmart.mapper.CodeMapper;
import com.wholesmart.mapper.entity.Code;
import com.wholesmart.service.CodeService;

/**
 * 数据字典服务接口实现
 * 
 * @author dyw
 * @date 2020年1月12日
 */
@Service
public class CodeServiceImpl implements CodeService {
	@Autowired
	private CodeMapper codeMapper;

	@Override
	public Code getById(Long id) {
		return null;
	}

	@Override
	public Page<Code> getPage(Map<String, Object> params) {
		Page<Code> resultPage = new Page<>();
		// page参数
		Object page = params.get("page");
		BeanUtils.copyProperties(page, resultPage);
		// 条件参数
		Object condition = params.get("condition");
		List<Code> records = codeMapper.selectPage(page, condition);
		long total = codeMapper.countForSelectPage(condition);
		resultPage.setRecords(records);
		resultPage.setTotal(total);
		return resultPage;
	}

	@Override
	public String save(Code entity) {
		if (entity.getId() == null) {
			codeMapper.insert(entity);
		} else {
			codeMapper.updateById(entity);
		}
		return entity.getId().toString();
	}

	@Override
	public void deleteById(Long id) {
		codeMapper.deleteById(id);
	}

	@Override
	public List<Map<String, String>> getCodeTree(String topCode) {
		return codeMapper.selectCodeTree(topCode);
	}

	@Override
	public List<Code> getSubCodesByPCode(String topCode) {
		return codeMapper.selectSubCodesByPCode(topCode);
	}

	@Override
	public void setNameById(Long id, String name) {
		codeMapper.updateNameById(id, name);
	}

	@Override
	public void setStatusById(Long id, String name) {
		codeMapper.updateStatusById(id, name);
	}
}
